package com.example.hao.dataencryption;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Base64;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.et_MD5text)
    EditText etMD5text;
    @BindView(R.id.btn_MD5Coding)
    Button btnMD5Coding;
    @BindView(R.id.tv_MD5)
    TextView tvMD5;
    @BindView(R.id.et_Base64text)
    EditText etBase64text;
    @BindView(R.id.btn_base64Coding)
    Button btnBase64Coding;
    @BindView(R.id.tv_Base64Coding)
    TextView tvBase64Coding;
    @BindView(R.id.btn_base64Decoding)
    Button btnBase64Decoding;
    @BindView(R.id.tv_Base64DeCoding)
    TextView tvBase64DeCoding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    /**
     * MD5 加密.原生MD5加密虽然不可逆.但已经有人运用大量加密后存储加密与未加密的结果.进行服务器验证.
     * 所以,一般可以在加密后 自己再添加一些字符串算法.实现属于自己的秘钥
     *
     * @param string
     * @return
     */
    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result.append(temp);
            }
            int length = result.length();

            // 在加密后的结果 自己加入以下 规则.添油加醋
//            for (int i = 0; i < length; i++) {
//                result.insert(i * 2 + 1, "X");
//            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }


    //Base64 的编码 与 解码

    /**
     * 解密
     *
     * @param base64Str
     * @return
     */
    public static String base64UTF_8Decrypt(String base64Str) {
        try {
            byte[] decode = Base64.decode(base64Str, Base64.DEFAULT);
            return new String(decode, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 加密
     *
     * @param str
     * @return
     */
    public static String base64UTF_8(String str) {
        if (TextUtils.isEmpty(str))
            return "";
        try {
            return Base64.encodeToString(str.getBytes("UTF-8"), Base64.DEFAULT);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";

    }



    @OnClick(R.id.btn_MD5Coding)
    public void onBtnMD5CodingClicked() {
        String s = etMD5text.getText().toString();
        String md5 = md5(s);
        tvMD5.setText(md5);
    }


    @OnClick(R.id.btn_base64Coding)
    public void onBtnBase64CodingClicked() {
        String s = etBase64text.getText().toString();
        String base64UTF_8 = base64UTF_8(s);
        tvBase64Coding.setText(base64UTF_8);
    }



    @OnClick(R.id.btn_base64Decoding)
    public void onBtnBase64DecodingClicked() {
        String text = (String) tvBase64Coding.getText();
        String s = base64UTF_8Decrypt(text);
        tvBase64DeCoding.setText(s);
    }


}
